package csgbd.Hotel.GUI;

import csgbd.Hotel.Common.DB.ConnectionFactory;
import csgbd.Hotel.Common.Entity.RoomType;
import csgbd.Hotel.Common.Facade.Default.HotelFacade;
import csgbd.Hotel.Common.Facade.IHotelFacade;
import java.sql.SQLException;
import java.util.ArrayList;
import java.util.logging.Level;
import java.util.logging.Logger;
import javax.swing.JOptionPane;
import javax.swing.UIManager;
import javax.swing.table.DefaultTableModel;

/**
 *
 * @author italopessoa
 */
public class jfrmRoomType extends javax.swing.JFrame {

    private ArrayList<RoomType> roomTypes;
    private IHotelFacade facade;
    /**
     * Creates new form jfrmRoomType
     */
    public jfrmRoomType() {
        initComponents();
        ConnectionFactory.OpenConnection();
        this.facade = new HotelFacade();
        this.showRoomTypes();
    }

    /**
     * This method is called from within the constructor to initialize the form.
     * WARNING: Do NOT modify this code. The content of this method is always
     * regenerated by the Form Editor.
     */
    @SuppressWarnings("unchecked")
    // <editor-fold defaultstate="collapsed" desc="Generated Code">//GEN-BEGIN:initComponents
    private void initComponents() {

        jbtnClear = new javax.swing.JButton();
        jbtnEditRoomType = new javax.swing.JButton();
        jbtnRemoveRoomType = new javax.swing.JButton();
        jbtnNewRoomType = new javax.swing.JButton();
        jScrollPane1 = new javax.swing.JScrollPane();
        jtbGuests = new javax.swing.JTable();
        jtxtName = new javax.swing.JTextField();
        jLabel2 = new javax.swing.JLabel();
        jLabel1 = new javax.swing.JLabel();
        jtxtId = new javax.swing.JTextField();

        setDefaultCloseOperation(javax.swing.WindowConstants.EXIT_ON_CLOSE);

        jbtnClear.setText("Clear");
        jbtnClear.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                jbtnClearActionPerformed(evt);
            }
        });

        jbtnEditRoomType.setText("Update");
        jbtnEditRoomType.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                jbtnEditRoomTypeActionPerformed(evt);
            }
        });

        jbtnRemoveRoomType.setText("Remove");
        jbtnRemoveRoomType.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                jbtnRemoveRoomTypeActionPerformed(evt);
            }
        });

        jbtnNewRoomType.setText("Save");
        jbtnNewRoomType.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                jbtnNewRoomTypeActionPerformed(evt);
            }
        });

        jtbGuests.setModel(new javax.swing.table.DefaultTableModel(
            new Object [][] {
                {null, null},
                {null, null},
                {null, null},
                {null, null}
            },
            new String [] {
                "ID", "Name"
            }
        ) {
            Class[] types = new Class [] {
                java.lang.Integer.class, java.lang.String.class
            };
            boolean[] canEdit = new boolean [] {
                false, false
            };

            public Class getColumnClass(int columnIndex) {
                return types [columnIndex];
            }

            public boolean isCellEditable(int rowIndex, int columnIndex) {
                return canEdit [columnIndex];
            }
        });
        jtbGuests.addMouseListener(new java.awt.event.MouseAdapter() {
            public void mouseClicked(java.awt.event.MouseEvent evt) {
                jtbGuestsMouseClicked(evt);
            }
        });
        jScrollPane1.setViewportView(jtbGuests);

        jLabel2.setText("Name:");

        jLabel1.setText("ID:");

        jtxtId.setEditable(false);
        jtxtId.setText("jTextField1");
        jtxtId.setEnabled(false);

        javax.swing.GroupLayout layout = new javax.swing.GroupLayout(getContentPane());
        getContentPane().setLayout(layout);
        layout.setHorizontalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(layout.createSequentialGroup()
                .addContainerGap()
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addGroup(layout.createSequentialGroup()
                        .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                            .addComponent(jLabel2)
                            .addComponent(jLabel1))
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                        .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                            .addComponent(jtxtId, javax.swing.GroupLayout.PREFERRED_SIZE, 68, javax.swing.GroupLayout.PREFERRED_SIZE)
                            .addComponent(jtxtName, javax.swing.GroupLayout.PREFERRED_SIZE, 259, javax.swing.GroupLayout.PREFERRED_SIZE)))
                    .addGroup(layout.createSequentialGroup()
                        .addComponent(jbtnNewRoomType)
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                        .addComponent(jbtnEditRoomType)
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                        .addComponent(jbtnRemoveRoomType)
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                        .addComponent(jbtnClear))
                    .addComponent(jScrollPane1, javax.swing.GroupLayout.PREFERRED_SIZE, 388, javax.swing.GroupLayout.PREFERRED_SIZE))
                .addContainerGap(javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE))
        );
        layout.setVerticalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(javax.swing.GroupLayout.Alignment.TRAILING, layout.createSequentialGroup()
                .addGap(20, 20, 20)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(jLabel1)
                    .addComponent(jtxtId, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
                .addGap(15, 15, 15)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(jLabel2)
                    .addComponent(jtxtName, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
                .addGap(18, 18, 18)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(jbtnNewRoomType)
                    .addComponent(jbtnEditRoomType)
                    .addComponent(jbtnRemoveRoomType)
                    .addComponent(jbtnClear))
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
                .addComponent(jScrollPane1, javax.swing.GroupLayout.PREFERRED_SIZE, 111, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addContainerGap(javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE))
        );

        pack();
    }// </editor-fold>//GEN-END:initComponents

    private void jbtnEditRoomTypeActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_jbtnEditRoomTypeActionPerformed
        RoomType roomType = new RoomType(Integer.parseInt(this.jtxtId.getText()));
        roomType.setName(this.jtxtName.getText());

        this.updateRoomType(roomType);
    }//GEN-LAST:event_jbtnEditRoomTypeActionPerformed

    private void jbtnRemoveRoomTypeActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_jbtnRemoveRoomTypeActionPerformed
        RoomType roomType = new RoomType(Integer.parseInt(this.jtxtId.getText()));
        this.removeRoomType(roomType);
    }//GEN-LAST:event_jbtnRemoveRoomTypeActionPerformed

    private void jbtnNewRoomTypeActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_jbtnNewRoomTypeActionPerformed
        RoomType roomType = new RoomType(this.jtxtName.getText());

        this.newRoomType(roomType);
        this.showRoomTypes();
    }//GEN-LAST:event_jbtnNewRoomTypeActionPerformed

    private void jtbGuestsMouseClicked(java.awt.event.MouseEvent evt) {//GEN-FIRST:event_jtbGuestsMouseClicked
        int linha = this.jtbGuests.getSelectedRow();
        if(linha >-1){
            this.jtxtId.setText(String.valueOf(this.jtbGuests.getValueAt(linha,0)));
            this.jtxtName.setText(String.valueOf(this.jtbGuests.getValueAt(linha,1)));
        }
    }//GEN-LAST:event_jtbGuestsMouseClicked

    private void jbtnClearActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_jbtnClearActionPerformed
        this.clearRoomTypeValues();
    }//GEN-LAST:event_jbtnClearActionPerformed

    /**
     * @param args the command line arguments
     */
    public static void main(String args[]) {
        /* Set the Nimbus look and feel */
        //<editor-fold defaultstate="collapsed" desc=" Look and feel setting code (optional) ">
        /* If Nimbus (introduced in Java SE 6) is not available, stay with the default look and feel.
         * For details see http://download.oracle.com/javase/tutorial/uiswing/lookandfeel/plaf.html 
         */
        try {
             javax.swing.UIManager.setLookAndFeel(UIManager.getSystemLookAndFeelClassName());
        } catch (ClassNotFoundException ex) {
            java.util.logging.Logger.getLogger(jfrmRoomType.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
        } catch (InstantiationException ex) {
            java.util.logging.Logger.getLogger(jfrmRoomType.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
        } catch (IllegalAccessException ex) {
            java.util.logging.Logger.getLogger(jfrmRoomType.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
        } catch (javax.swing.UnsupportedLookAndFeelException ex) {
            java.util.logging.Logger.getLogger(jfrmRoomType.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
        }
        //</editor-fold>

        /* Create and display the form */
        java.awt.EventQueue.invokeLater(new Runnable() {
            public void run() {
                new jfrmRoomType().setVisible(true);
            }
        });
    }
    // Variables declaration - do not modify//GEN-BEGIN:variables
    private javax.swing.JLabel jLabel1;
    private javax.swing.JLabel jLabel2;
    private javax.swing.JScrollPane jScrollPane1;
    private javax.swing.JButton jbtnClear;
    private javax.swing.JButton jbtnEditRoomType;
    private javax.swing.JButton jbtnNewRoomType;
    private javax.swing.JButton jbtnRemoveRoomType;
    private javax.swing.JTable jtbGuests;
    private javax.swing.JTextField jtxtId;
    private javax.swing.JTextField jtxtName;
    // End of variables declaration//GEN-END:variables

    private void getRoomTypes(){
        try {
            this.roomTypes = this.facade.SelectAllRoomTypes();
        } catch (SQLException ex) {
            Logger.getLogger(jfrmRoomType.class.getName()).log(Level.SEVERE, null, ex);
        } catch (Exception ex) {
            Logger.getLogger(jfrmRoomType.class.getName()).log(Level.SEVERE, null, ex);
        }
    }
    
    private void showRoomTypes() {
        this.getRoomTypes();
        DefaultTableModel model = (DefaultTableModel) this.jtbGuests.getModel();
        model.setNumRows(0);

        for (RoomType roomType : this.roomTypes) {
            model.addRow(new Object[]{roomType.getId(), roomType.getName()});
        }
    }
    
    private void newRoomType(RoomType roomType){
        try {        
            this.facade.NewRoomType(roomType);
            JOptionPane.showMessageDialog(null, "Guest saved sucefull!");
        } catch (SQLException ex) {
            Logger.getLogger(jfrmRoomType.class.getName()).log(Level.SEVERE, null, ex);
        } catch (Exception ex) {
            Logger.getLogger(jfrmRoomType.class.getName()).log(Level.SEVERE, null, ex);
        }
    }
    
    private void removeRoomType(RoomType roomType){
        try {
            this.facade.RemoveRoomType(roomType);
            JOptionPane.showMessageDialog(null, "Guest removed sucefull!");
        } catch (SQLException ex) {
            Logger.getLogger(jfrmRoomType.class.getName()).log(Level.SEVERE, null, ex);
        } catch (Exception ex) {
            Logger.getLogger(jfrmRoomType.class.getName()).log(Level.SEVERE, null, ex);
        }
        
        this.showRoomTypes();
        this.clearRoomTypeValues();
    }
    
    private void updateRoomType(RoomType roomType){
        try {
            this.facade.UpdateRoomType(roomType);
            JOptionPane.showMessageDialog(null, "Guest updated sucefull!");
        } catch (SQLException ex) {
            Logger.getLogger(jfrmRoomType.class.getName()).log(Level.SEVERE, null, ex);
        } catch (Exception ex) {
            Logger.getLogger(jfrmRoomType.class.getName()).log(Level.SEVERE, null, ex);
        }
        
        this.showRoomTypes();
        this.clearRoomTypeValues();
    }
    
    private void clearRoomTypeValues(){
        this.jtxtId.setText("");
        this.jtxtName.setText("");
    }
}
